<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\service\NodeService;
use library\Controller;
use library\tools\Data;
use think\Console;
use think\Db;
use think\Request;
use think\exception\HttpResponseException;

/**
 * 广告管理
 * Class Index
 * @package app\admin\controller
 */
class Banner extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    protected $table = 'WebBanner';
   
    /**
     * 广告列表
     * @auth true
      * @menu true
     */
    public function index()
    {

        $this->title = ' 广告列表';
        $id=$this->request->param('id', 0);
        $data['tid']=$id;
        $query = $this->_query($this->table)
                ->where($data)
                ->like('title')
                ->equal('status');
        $query->order('sort desc,id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {
        $this->clist = Db::name('WebBannerType')->select();
        foreach ($data as &$vo) {
            $vo['start_time']=date("Y-m-d",$vo['start_time']);
            $vo['end_time']=date("Y-m-d",$vo['end_time']);
            $vo['created_at']=date("Y-m-d",$vo['created_at']);
            list($vo['cate']) = [[]];
            foreach ($this->clist as $cate) if ($cate['id'] === $vo['tid']) $vo['cate'] = $cate;
        }
    }

    /**
     * 添加广告
     * @auth true
     */
    public function bannerAdd()
    {   

        $this->title = '添加广告';  //这里是广告添加 
        $this->_form($this->table, 'banner_add');
       
    }
    protected function _form_filter(&$data)
    {

        if ($this->request->isGet()) {
            $this->cates = Db::name('WebBannerType')
                        ->where([ 'status' => '1'])
                        ->order('sort desc,id desc')
                        ->select();
            $this->areas = Db::name('SystemAgentUser')
                        ->alias('sau')
                        ->join("Area a",'sau.provinces_id=a.area_id')
                        ->join("Area ae",'sau.city_id=ae.area_id')
                        ->where([ 'sau.pid' => '0'])
                        ->where([ 'sau.status' => '1'])
                        ->where([ 'sau.is_deleted' => '0'])
                        ->field("sau.*,a.area_name as province,ae.area_name as city")
                        ->order('sau.id desc')
                        ->select();
        }elseif ($this->request->isPost()) {

            $data['start_time']=strtotime($data['start_time']);
            $data['end_time']=strtotime($data['end_time']);

        }
        

    }
    /**
     * 表单结果处理
     * @param boolean $result
     */
    protected function _form_result($result)
    {
        if ($result && $this->request->isPost()) {
            $this->success('操作成功！');
        }
    }
    /**
     * 编辑广告
     * @auth true
     */
    public function edit(){

        $this->title="编辑广告";
        $this->_form($this->table, 'banner_add');
    }

    /**
     * 禁用广告
     * @auth true
     */
     public function forbid()
    {
        $this->_save($this->table, ['status' => '0']);
    }

    /**
     * 启用广告
     * @auth true
     */
    public function resume()
    {
        $this->_save($this->table, ['status' => '1']);
    }

    

}
